package com.hk.aefz.web;


import com.hk.aefz.po.Comment;
import com.hk.aefz.service.CommentService;
import com.hk.aefz.vo.CommentAdmin;
import com.hk.aefz.vo.PageResult;
import com.hk.aefz.vo.ResultMsg;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.security.auth.Subject;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author 阿尔法哲
 * @since 2020-07-21
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @ApiOperation("跳转到评论管理")
    @GetMapping(path = "/pl-admin")
    public ModelAndView plAdmin(
            @RequestParam(value = "page",defaultValue = "0") Integer page,
            @RequestParam(value = "size",defaultValue = "5") Integer size) {
        ModelAndView mv = new ModelAndView("/blogafter/adminComment");
        //        获取认证的账户id
        final int aid = (int) SecurityUtils.getSubject().getSession().getAttribute("aid");
        PageResult<CommentAdmin> commentAdmins = commentService.selectList(page,size,aid);
        mv.addObject("commentAdmins",commentAdmins);
        return mv;
    }

    @ApiOperation("发表评论")
    @PostMapping(path = "publish")
    public ResultMsg publishComment(@RequestBody Comment comment) {
//        System.out.println("查询到的数据=="+comment);
        ResultMsg resultMsg = new ResultMsg();
        boolean boo = commentService.save(comment);
        if (boo) {
            resultMsg.setState(201);
            resultMsg.setMsg("发表成功");
        } else {
            resultMsg.setState(400);
            resultMsg.setMsg("发表失败,请稍后重试!");
        }
        return resultMsg;
    }

    @ApiOperation("查询所在博客所有评论")
    @PostMapping(path = "select-byeid")
    public PageResult<Comment> seleectByEid(
            Integer eid,
            @RequestParam(value = "page",required = true,defaultValue = "1") Integer page,
            @RequestParam(value = "size",required = true,defaultValue = "5") Integer size) {
        PageResult<Comment> pageResult = commentService.selectByEid(eid,page,size);
        return pageResult;
    }

}

